/**
 * 
 */
package dms.presentation.action.student;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.MappingDispatchAction;

import dms.dto.StudentDTO;
import dms.model.role.Student;
import dms.model.role.wrapper.StudentWrapper;
import dms.util.Constant;
import dms.util.ContextUtil;

/**
 * @author 李健
 *
 */
public class InformationAction extends StudentBaseAction {
	public ActionForward personal(ActionMapping mapping,
								  ActionForm form,
								  HttpServletRequest request,
								  HttpServletResponse response) {
		StudentWrapper studentWrapper = (StudentWrapper) ContextUtil.getContext().getBean("studentWrapper");
		Student student = studentWrapper.getStudent();
		request.setAttribute("student", student);
		request.setAttribute("dormitory", student.getDormitory());
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward edit(ActionMapping mapping,
			  ActionForm form,
			  HttpServletRequest request,
			  HttpServletResponse response) {
		StudentWrapper studentWrapper = (StudentWrapper) ContextUtil.getContext().getBean("studentWrapper");
		Student student = studentWrapper.getStudent();
		request.setAttribute("student", student);
		request.setAttribute("dormitory", student.getDormitory());
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward modify(ActionMapping mapping,
								  ActionForm form,
								  HttpServletRequest request,
								  HttpServletResponse response) {
		ActionErrors errors = new ActionErrors();
		StudentDTO studentDTO = (StudentDTO) form;
		StudentWrapper studentWrapper = this.getStudentWrapper();
		try {
			studentWrapper.modifyPersonalInfo(studentDTO);
		} catch (Exception e) {
			errors.add(Globals.ERROR_KEY, new ActionMessage(e.getMessage(), false));
			this.saveErrors(request, errors);
			return mapping.getInputForward();
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward mainFrame(ActionMapping mapping,
			  ActionForm form,
			  HttpServletRequest request,
			  HttpServletResponse response) {
		
	}
}
